package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunSleepConfigMapper;
import com.csun.cmny.provider.model.constant.LifeConstant;
import com.csun.cmny.provider.model.domain.DmcCsunSleepConfig;
import com.csun.cmny.provider.model.domain.DmcCsunSleepThreshold;
import com.csun.cmny.provider.model.domain.DmcDeviceUserBind;
import com.csun.cmny.provider.model.dto.life.DfcSleepThresholdDto;
import com.csun.cmny.provider.model.vo.DfcSleepThresholdVo;
import com.csun.cmny.provider.service.DmcCsunSleepThresholdService;
import com.csun.cmny.provider.service.DmcDeviceUserBindService;
import com.csun.cmny.provider.service.DmcDfcSleepThresholdService;
import com.csun.cmny.util.PublicUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: ZNR
 * @Date: 2020-12-16
 * @Description: 德福长床垫阈值设置
 */

@Service
public class DmcDfcSleepThresholdServiceImpl extends BaseService<DmcCsunSleepConfig> implements DmcDfcSleepThresholdService {
    @Resource
    private DmcCsunSleepConfigMapper dmcCsunSleepConfigMapper;
    @Resource
    private DmcCsunSleepThresholdService dmcCsunSleepThresholdService;
    @Resource
    private DmcDeviceUserBindService deviceUserBindService;

    @Override
    public DfcSleepThresholdVo getDfcSleepThreshold(Long deviceId) {

        DfcSleepThresholdVo vo = new DfcSleepThresholdVo();
        DmcCsunSleepConfig dmcCsunSleepConfig = dmcCsunSleepConfigMapper.selectByPrimaryKey(deviceId);
        DmcCsunSleepThreshold dmcCsunSleepThreshold = dmcCsunSleepThresholdService.selectByKey(deviceId);
        if(dmcCsunSleepConfig != null){
            BeanUtils.copyProperties(dmcCsunSleepConfig,vo);
        }
        if(dmcCsunSleepThreshold != null){
            BeanUtils.copyProperties(dmcCsunSleepThreshold,vo);
        }
        return vo;
    }

    @Override
    public int setDfcSleepThreshold(DfcSleepThresholdDto dto, LoginAuthDto loginAuthDto) {
        List<Long> deviceIdList = dto.getDeviceIdList();
        int i = 0;

        for (Long deviceId : deviceIdList) {

            //1.下发时间指令
            DmcCsunSleepConfig csunHumidistatConfig = selectByKey(deviceId);
            DmcCsunSleepConfig config = new DmcCsunSleepConfig();

            config.setDeviceId(deviceId);
            config.setVitalMeasuringTimeInterval(dto.getVitalMeasuringTimeInterval());
            config.setIsSend(LifeConstant.DEVICE_ORDER_NEED_SEND);
            config.setLastOperator(loginAuthDto.getUserName());
            config.setLastOperatorId(loginAuthDto.getUserId());

            if(csunHumidistatConfig == null){
                config.setCreator(loginAuthDto.getUserName());
                config.setCreatorId(loginAuthDto.getUserId());
                dmcCsunSleepConfigMapper.insertSelective(config);
            }else{
                dmcCsunSleepConfigMapper.updateByPrimaryKeySelective(config);
            }

            //2.设置阈值
            DmcCsunSleepThreshold dmcCsunSleepThreshold = dmcCsunSleepThresholdService.selectByKey(deviceId);

            //同步到个人用户设备绑定表
            DmcDeviceUserBind deviceUserBind = new DmcDeviceUserBind();
            deviceUserBind.setDeviceId(deviceId);
            DmcDeviceUserBind userBind = deviceUserBindService.selectOne(deviceUserBind);

            if (PublicUtil.isNotEmpty(userBind)){
                BeanUtils.copyProperties(dto,deviceUserBind);
                deviceUserBindService.update(deviceUserBind);
            }


            DmcCsunSleepThreshold threshold = new DmcCsunSleepThreshold();

            threshold.setDeviceId(deviceId);
            BeanUtils.copyProperties(dto,threshold);
            threshold.setLastOperator(loginAuthDto.getUserName());
            threshold.setLastOperatorId(loginAuthDto.getUserId());

            if(dmcCsunSleepThreshold == null){
                threshold.setCreator(loginAuthDto.getUserName());
                threshold.setCreatorId(loginAuthDto.getUserId());
                dmcCsunSleepThresholdService.save(threshold);
            }else{
                dmcCsunSleepThresholdService.update(threshold);
            }
            i++;
        }
        return i;
    }
}
